<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notepad</title>

    <!-- bootstrap -->
    <link rel="stylesheet" href="./assets/css/bootstrap.min.css" />

    <!-- MUI -->
    <link rel="stylesheet" href="./assets/plugins/mui/css/mui.min.css" />

    <!-- animate.css -->
    <link rel="stylesheet" href="./assets/css/animate.min.css" />

    <!-- 引入bootstrapValidator表单验证插件 -->
    <link rel="stylesheet" href="./assets/plugins/bootstrap-validator/bootstrapValidator.min.css">

    <!-- 重置样式 -->
    <link rel="stylesheet" href="./assets/css/reset.css" />

    <!-- 公共样式 -->
    <link rel="stylesheet" href="./assets/css/common.css" />

    <!-- 页面独立的 -->
    <link rel="stylesheet" href="./assets/css/UserLogin.css" />

    <!-- jquery  -->
    <script src="./assets/js/jquery.min.js"></script>

    <!-- 引入bootstrapValidator表单验证插件 -->
    <script src="./assets/plugins/bootstrap-validator/bootstrapValidator.min.js"></script>
</head>
<body>
    <!-- 用户信息 -->
    <div class="user">
        <div class="avatar wow fadeInUp">
            <img src="./assets/images/avatar.png" />
        </div>

        <div class="notice wow fadeInUp" data-wow-delay="100ms">注册账号</div>
    </div>

    <form id="register" class="login wow fadeInUp" data-wow-delay="200ms">
        <div class="form-group">
            <label for="mobile">手机号码</label>
            <input type="text" class="form-control" name="mobile" id="mobile" placeholder="请输入手机号码" required />
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" class="form-control" name="password" id="password" placeholder="请输入密码" required />
        </div>
        
        <div class="action">
            <button type="submit" name="submit" class="btn">注册</button>
        </div>
    </form>
</body>
</html>
<!-- wow.js -->
<script src="./assets/js/wow.min.js"></script>

<!-- bootstrap -->
<script src="./assets/js/bootstrap.min.js"></script>

<!-- MUI -->
<script src="./assets/plugins/mui/js/mui.min.js"></script>

<!-- axios请求 -->
<script src="./assets/plugins/axios/axios.min.js"></script>
<script src="./assets/plugins/axios/request.js"></script>

<!-- cookie -->
<script src="./assets/plugins/cookie.js"></script>

<script>
    //实例化wow.js
    new WOW().init()

    //获取cookie信息
    var business = cookie.get('business') ? JSON.parse(cookie.get('business')) : {};

    if(business && JSON.stringify(business) != "{}")
    {
        mui.toast('无须重复登录', {duration: 1000})
        setTimeout(() => {
            location.href = 'UserIndex.html'
        }, 1000)
    }

    //表单验证
    $('#register').bootstrapValidator({
        // 所有字段的默认错误消息
        message: '表单内容不能为空',
        //字段校验图标。默认bootstrap图标
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        // 验证字段
        fields: {
            mobile: {
                message: '手机号码验证失败',
                validators: {
                    notEmpty: {
                        message: '手机号码不能为空'
                    },
                    stringLength: {
                        min: 11,
                        max: 11,
                        message: '手机号码长度为11位'
                    },
                    regexp: {
                        regexp: /^1[3,4,5,6,7,8,9][0-9]{9}$/,
                        message: '手机号码格式有误'
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: '请输入密码'
                    },
                }
            },
        }
    }).on('success.form.bv', function(e){
        //阻止多次提交
        e.preventDefault()
    })
    
    $('#register').on('submit', async function(e){
        //通过表单验证后再提交
        if(!$("form").data('bootstrapValidator').isValid())
        {
            //重置表单
            $('form').data('bootstrapValidator').resetForm();
            return false
        }

        //组装数据提交
        var data = {
            mobile: $.trim($('#mobile').val()),
            password: $.trim($('#password').val())
        }

        var result = await POST({
            url: '/business/register',
            params: data
        })

        if(result.code == 1)
        {
            mui.toast(result.msg, {duration: 1000})
            setTimeout(() => {
                location.href = 'login.html'
            }, 1000)
        }else
        {
            //重置表单
            $('form').data('bootstrapValidator').resetForm()
            mui.toast(result.msg)
            return false
        }
    })
</script>